<@pp.dropOutputFile />
<#global tables = doc.Model.o_RootObject.c_Children.o_Model.c_Tables>
<#global references = doc.Model.o_RootObject.c_Children.o_Model.c_References>

<#list tables.o_Table as t>
		<#assign columns = t.c_Columns>
		<#--获取主键列-->	
		<#assign pkId = t.c_Keys.o_Key["c_Key.Columns"].o_Column[0].@Ref>
		<#assign pk = doc.Model.o_RootObject["//o_Column[@Id=$pkId]"]>
	
<@pp.changeOutputFile name="/modular/models/" + f1(t.a_Code) + ".php" />	
<?php			
/**
 * ${f1(t.a_Code)} 
 * 
 * @uses Zend_Db_Table_Abstract
 * @package zfgen_demo
 * @version 1.0
 * @copyright 2006-2007 suwu
 * @author suwu <suhw@qq.com> 
 *
 */
class ${f1(t.a_Code)} extends Zend_Db_Table_Abstract
	{	
/**
 *列名       中文        类型
<#list columns.o_Column as c>
 *${c.a_Code}	${c.a_Name}	${c.a_DataType}
</#list>
*/
	  //protected $_schema = schema;
		protected $_name = '${t.a_Code}';
		protected $_primary = '${pk[0].a_Code}';	 
		private $_request;
		private $_pager;
		
	  protected function _setup()
		{
			//$config = Zend_Registry::get('config');
			$this->_request = new Zend_Controller_Request_Http();
	 		$this->_pager = array(
	 				'pRecordTotal' => 0,
	 				'pSize' => 0,
	 				'pCurrent' => 1,
	 				'pTotal' => 0,
	 				'pPre' => 0,
	 				'pNext' => 0,
	 				'pN' => 10,
	 				'pPreN' => 0,
	 				'pNextN' => 0, 
	 				'pUrl' = > '/p'
	 				);
			parent::_setup();
		}
/*
记录总数pRecordTotal
每页大小pSize
当前页pCurrent
总页数pTotal
总一页：pPre
后一页：pNext
索引个数：pN   用于 1 2 3 4 5风格的导航
前N页:pPreN
后N页:pNextN
baseUrl:pUrl     由module/controller/action/p组成
*/	
	 	public function setPager($p = null)
	 	{
	 		if(isset($p))
	 		{
	 			array_merge($this->_pager, (array)$p);
	 		}
	 	}	
		<#--表在xml中的id号-->
		<#assign tId = t.@Id>
		<#--获取作为父表的关联关系-->
		<#assign ptList = doc.Model["//c_References/o_Reference/c_Object1/o_Table[@Ref=$tId]/ancestor::o_Reference"]>
		<#if ptList?size != 0>
		/*
			protected $_dependentTables = array(
			<#list ptList as pt>			
				<#--获取子表-->
				<#assign ctId = pt.c_Object2.o_Table.@Ref>
				<#assign cTable = doc.Model["//c_Tables/o_Table[@Id = $ctId]"]>				
				'${cTable[0].a_Code}'<#if pt_has_next>,</#if>					
			</#list>
			);
		*/
		</#if>
		
		<#--获取作为子表的关联关系-->
		<#assign ctList = doc.Model["//c_References/o_Reference/c_Object2/o_Table[@Ref=$tId]/ancestor::o_Reference"]>
		
		<#if ctList?size != 0 >
		
		protected $_referenceMap = array(
		<#list ctList as ct>			
			<#--获取父表-->
			<#assign ptId = ct.c_Object1.o_Table.@Ref>
			<#assign pTable = doc.Model["//c_Tables/o_Table[@Id = $ptId]"]>
			
			<#--父表表名-->				
			<#--父表的关联列-->
			<#assign pcId = ct.c_Joins.o_ReferenceJoin.c_Object1.o_Column.@Ref>
			<#assign pColumn = doc.Model["//o_Column[@Id = $pcId]"]>
				
			<#--本表的关联列-->
			<#assign slefId = ct.c_Joins.o_ReferenceJoin.c_Object2.o_Column.@Ref>
			<#assign slefColumn = doc.Model["//o_Column[@Id = $slefId]"]> 	
				
			'${pTable[0].a_Code}_${pColumn[0].a_Code}' => array(
				'columns' =>'${slefColumn[0].a_Code}', 
				'refTableClass' => '${f1(pTable[0].a_Code)}',
				'refColumns' => '${pColumn[0].a_Code}'
				)<#if ct_has_next>,</#if>
		</#list>		
			);
		</#if>
		
		
//add     保存实体
	public function save()
	{
		if ($this->_request->isPost()) {
		{
			$filter           = new Zend_Filter_StripTags();
		<#list columns.o_Column as c>
			
			<@compress single_line=true><#switch c.a_DataType?substring(0, 3)>
				<#case "big">
					row['${c.a_Code}'] = (int)$this->_request->getPost('${c.a_Code}');
				<#break>
				<#case "sma">
					row['${c.a_Code}'] = (int)$this->_request->getPost('${c.a_Code}');
				<#break>
				<#case "int">
					row['${c.a_Code}'] = (int)$this->_request->getPost('${c.a_Code}');
				<#break>
				<#case "var">
					row['${c.a_Code}'] = $filter->filter($this->_request->getPost('${c.a_Code}');
				<#break>
				<#case "dat">
					row['${c.a_Code}'] = $_SERVER['REQUEST_TIME'];
				<#break>
				<#case "tim">
					row['${c.a_Code}'] = $_SERVER['REQUEST_TIME'];
				<#break>					
				<#default>
					row['${c.a_Code}'] = $this->_request->getPost('${c.a_Code}');
				<#break>
			</#switch></@compress>			
		</#list>
		
			if (isset(row['${pk[0].a_Code}'])
			{
				$pk = $this->update($row);
			}
			else
			{
				$pk = $this->insert($row);
			}	
			return $pk;	
		}
		
		public function del($id)
		{
		  $where = $this->getAdapter()->quoteInto('${pk[0].a_Code} = ?', $id);
			return $this->delete($where);		
		}
		
		public function list($p = null,$where = null, $order = null)
		{
			
	 		array_merge(_pager, (array)$p);
			if (_pager['pSize'] == 0)
			{//表示要获取所有记录
				$total = $this->fetchAll($where, $order)->count();
				$rows = $this->fetchAll($where, $order)->toArray();
				
				$this->_pager['pRecordTotal'] = $total;
				$rows['pager'] = $this->_pager;	
			}
			else
			{
				$total = $this->fetchAll($where, $order)->count();
				$offset = $this->_pager['pSize'] * ($this->_pager['pCurrent'] - 1); //记录偏移量
				$rows = $this->fetchAll($where, $order, _pager['pSize'], $offset)->toArray();
				
				$this->_pager['pRecordTotal'] = $total;
				$this->calPager();
				$rows['pager'] = $this->_pager;				
			}		
			return $rows;
		}
/*
记录总数pRecordTotal
每页大小pSize
当前页pCurrent
总页数pTotal
总一页：pPre
后一页：pNext
索引个数：pN   用于 1 2 3 4 5风格的导航
前N页:pPreN
后N页:pNextN
baseUrl:pUrl     由module/controller/action/p组成
*/			
		private function calPager();
		{
			if ($this->_pager['pSize'] != 0)
				$this->_pager['pTotal'] = ceil($this->_pager['pTotal'] / $this->_pager['pSize']);
			if ($this->__pager['pCurrent'] > 1)	
				$this->_pager['pPre'] = $this->_pager['pCurrent'] - 1;
			else
				$this->_pager['pPre'] = $this->_pager['pCurrent'];	
			
			if ($this->_pager['pCurrent'] < $this->_pager['pTotal'])	
				$this->_pager['pNext'] = $this->_pager['pCurrent'] + 1;
			else
				$this->_pager['pNext'] = $this->_pager['pCurrent'];		
				
			if ($this->_pager['pCurrent'] - ceil($this->_pager['pN'] / 2) >= 0)
				$this->_pager['pPreN'] = $this->_pager['pCurrent'] - ceil($this->_pager['pN'] / 2);
			
			
			if ($this->_pager['pCurrent'] + ceil($this->_pager['pN'] / 2) <= $this->_pager['pTotal'])
				$this->_pager['pNextN'] = $this->_pager['pCurrent'] - ceil($this->_pager['pN'] / 2);
			else
				$this->_pager['pNextN'] = $this->_pager['pTotal'];	
		} 
	}
	
	public function one($id)
	{
		return $this->find($id)->toArray();
	}
  <#list ctList as ct>			
			<#--获取父表-->
			<#assign ptId = ct.c_Object1.o_Table.@Ref>
			<#assign pTable = doc.Model["//c_Tables/o_Table[@Id = $ptId]"]>
			
			<#--父表表名-->				
			<#--父表的关联列-->
			<#assign pcId = ct.c_Joins.o_ReferenceJoin.c_Object1.o_Column.@Ref>
			<#assign pColumn = doc.Model["//o_Column[@Id = $pcId]"]>
				
			<#--本表的关联列-->
			<#assign slefId = ct.c_Joins.o_ReferenceJoin.c_Object2.o_Column.@Ref>
			<#assign slefColumn = doc.Model["//o_Column[@Id = $slefId]"]> 	
	public function get${f1(pTable[0].a_Code)}($id)
	{//id为对象的主键值，先由主建找到外建主，再找到父对象
		$c = $this->one($id);
		$p = new ${f1(pTable[0].a_Code)}();
		$where = $this->getAdapter()->quoteInto('${pColumn[0].a_Code} = ?', $c['${slefColumn[0].a_Code}']);
		
		return $p->fetchRow($where)->toArray();
	}
	
	public function get${f1(pTable[0].a_Code)}ByPid($pid)
	{
		$p = new ${f1(pTable[0].a_Code)}();
		return $p->find($pid)->toArray();		
	}
	
	public function get${f1(pTable[0].a_Code)}List($p = null,$where = null, $order = null)
	{
		$p = new ${f1(pTable[0].a_Code)}();	
		return $p->list($p, $where, $order);
	}
  </#list>	
	
	<#list ptList as pt>			
				<#--获取子表-->
				<#assign ctId = pt.c_Object2.o_Table.@Ref>
				<#assign cTable = doc.Model["//c_Tables/o_Table[@Id = $ctId]"]>		
			<#--子表表名-->				
			<#--子表的关联列-->
			<#assign ccId = pt.c_Joins.o_ReferenceJoin.c_Object2.o_Column.@Ref>
			<#assign cColumn = doc.Model["//o_Column[@Id = $ccId]"]>
	public function get${f1(cTable[0].a_Code)}List($p = null,$id = null, $order = null)
	{
		$where = $this->getAdapter()->quoteInto('${cColumn[0].a_Code} = ?', $id);
		
		$c = new ${f1(cTable[0].a_Code)};
		return $c->list($p, $where, $order);
	}				
										
	</#list>
}
</#list>

<#function f1 varString>
	<#local p = varString?split("_")>
	<#local retValue = "">
		<#list p as o>
			<#local retValue = retValue?cap_first + o?cap_first>
		</#list>
	<#return retValue>	
</#function> 